<?php
namespace App\Service\Chat;

use App\Constants\Chat\RedisKey;
use App\Constants\Chat\WsMessage;
use App\Foundation\Traits\Singleton;
use App\Model\Chat\FriendChatHistory;
use App\Model\Chat\GroupChatHistory;
use App\Pool\Redis;
use App\Service\BaseService;


/**
 * 聊天初始化服务类
 * Class MessageService
 * @package App\Service\Setting
 * @Author YiYuan-Lin
 * @Date: 2021/3/12
 */
class InitService extends BaseService
{
    use Singleton;

    /**
     * 获取初始化聊天信息
     * @return array
     */
    public function initialization() : array
    {
        $returnUserInfo = ['id'=>0,'displayName'=>'游客','avatar'=>''];
        $userInfo = context_get('user_info');
        if($userInfo){
            $returnUserInfo['id'] = $userInfo['id'];
            $returnUserInfo['displayName'] = $userInfo['desc'];
            $returnUserInfo['avatar'] = $userInfo['avatar'];
        }
        return [
            'action' => WsMessage::MESSAGE_TYPE_INIT,
            'user_info' => $returnUserInfo,
        ];
    }

    /**
     * 根据用户获取最后一条信息以及未读信息
     * @param array $user
     * @param array $currentUserInfo
     * @return array
     */
    private function getUnReadMessageByUser(array $user, array $currentUserInfo) : array
    {
        if (empty($user)) return [];

        $lastMessage = FriendChatHistory::query()
            ->where(function ($query) use ($currentUserInfo, $user) {
                $query->where('from_uid', $currentUserInfo['id'])->where('to_uid', $user['id']);
            })->orWhere(function ($query) use ($currentUserInfo, $user) {
                $query->where('from_uid', $user['id'])->where('to_uid', $currentUserInfo['id']);
            })
            ->orderBy('send_time', 'desc')
            ->first();

        $unread = FriendChatHistory::query()
            ->where(function ($query) use ($currentUserInfo, $user) {
                $query->where('from_uid', $user['id'])->where('to_uid', $currentUserInfo['id'])
                    ->where('reception_state', FriendChatHistory::RECEPTION_STATE_NO);
            })
            ->orderBy('send_time', 'desc')
            ->count();

        return [
            'unread' => $unread,
            'lastContent' => $lastMessage['content'] ?? '',
            'lastSendTime' => intval($lastMessage['send_time'] ?? 0),
            'lastContentType' => $lastMessage['type'] ?? ''
        ];
    }

    /**
     * 根据组获取最后一条信息以及未读信息
     * @param array $groupInfo
     * @param array $currentUserInfo
     * @return array
     */
    private function getUnReadMessageByGroup(array $groupInfo, array $currentUserInfo) : array
    {
        if (empty($currentUserInfo)) return [];
        $unread = Redis::getInstance()->sCard(RedisKey::GROUP_CHAT_UNREAD_MESSAGE_BY_USER . $currentUserInfo['id']);
        Redis::getInstance()->del(RedisKey::GROUP_CHAT_UNREAD_MESSAGE_BY_USER . $currentUserInfo['id']);

        $lastMessage = GroupChatHistory::query()->where('to_group_id', $groupInfo['group_id'])->orderBy('send_time', 'desc')->first();
        return [
            'unread' => $unread,
            'lastContent' => $lastMessage['content'] ?? '',
            'lastSendTime' => intval($lastMessage['send_time']) ?? 0,
            'lastContentType' => $lastMessage['type'] ?? ''
        ];
    }
}
